package hc;

import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spi.properties.ClusterProperty;
import com.hazelcast.topic.ITopic;

import java.time.Instant;

public class P {

    static PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    private HazelcastInstance hz;

    public P(String publicAddress, int port, String member) {
        Config config = new Config();
        config.getNetworkConfig().setPublicAddress(publicAddress).setPort(port);
        config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true)
                .addMember(member);
        config.setProperty(ClusterProperty.EVENT_THREAD_COUNT.getName(), "10");

        hz = Hazelcast.newHazelcastInstance(config);
    }

    public void start() throws InterruptedException {
        ITopic<Integer> topic = hz.getTopic("topic");

        performanceAnalyze.start();

        while (true) {
            Thread.sleep(1000);
            for (int i = 0; i < 100; i++) {
                topic.publish(Instant.now().getNano());
                performanceAnalyze.cpCounter();
            }


//            try {
//                if(performanceAnalyze.meter.getMeanRate() > 200000) {
//                    Thread.sleep(0, 1);
//                }
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }
}
